package com.shanghaionstar.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import com.onstar.cn.SharedPoiItemInfo;
import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.shanghaionstar.activity.webview.handler.CommonWebViewActivity;
import com.shanghaionstar.activity.webview.handler.WebDataEntity;

import java.util.List;

/**
 * Created by 7828wc on 16/8/17.
 */
public class SharePartnerActivity extends BaseActivity {
    LogManager Log = LogManager.getInstance();

    @Override
    public BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogManager.getInstance().d("into SharePartnerActivity...............");

        Uri uri = getIntent().getData();
        if (getIntent().getData().toString().contains("ShareService")) {
            pareseUriToPoi(Uri.parse(uri.toString()).getQuery());
        } else if (getIntent().getData().toString().contains("ovdReport")) {
            Intent intent = new Intent(this, LoadingActivity.class);
            intent.putExtra(Constants.PARTNER_AUTONAV_FLAG, true);
            intent.putExtra(Constants.PARTNER_3RD_FLAG, "OVDEmailFragment");
            startActivity(intent);
            finish();
        }  else {
            Intent intent = new Intent(this, LoadingActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        this.setIntent(intent);
    }

    //poiName=上海市徐汇区田林路384号&latitude=31.16765367831064&longitude=121.39876350760461&phoneNo=&address=在新业大楼附近&province=&city=&poiDescription=&autoSendToTBT=autoSend
    private void pareseUriToPoi(String url) {
        SharedPoiItemInfo poiItemInfo = new SharedPoiItemInfo();
        String[] keyValues = url.split("&");
        for (int i = 0; i < keyValues.length; i++) {
            String[] array = keyValues[i].split("=");
            String key = array[0];
            String value = "";
            if (array.length > 1) {
                value = array[1];
            }
            if (key.equalsIgnoreCase("poiName")) {
                poiItemInfo.setTitle(value);
                continue;
            }
            if (key.equalsIgnoreCase("phoneNo")) {
                poiItemInfo.setTelphone(value);
                continue;
            }
            if (key.equalsIgnoreCase("longitude")) {
                poiItemInfo.setLongitude(Double.parseDouble(value));
                continue;
            }
            if (key.equalsIgnoreCase("latitude")) {
                poiItemInfo.setLatitude(Double.parseDouble(value));
                continue;
            }
            if (key.equalsIgnoreCase("address")) {
                poiItemInfo.setAddress(value);
                continue;
            }
            if (key.equalsIgnoreCase("city")) {
                poiItemInfo.setCityName(value);
                continue;
            }
            if (key.equalsIgnoreCase("citycode")) {
                poiItemInfo.setCityName(value);
                continue;
            }
            if (key.equalsIgnoreCase("autoSendToTBT")) {
                if (value.equalsIgnoreCase("false"))
                    poiItemInfo.setAutoSendTBT(false);
                else
                    poiItemInfo.setAutoSendTBT(true);
                poiItemInfo.setSharedValue(key);
                continue;
            }
        }
        Intent intent = new Intent(this, LoadingActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean(Constants.PARTNER_AUTONAV_FLAG, true);
        bundle.putString(Constants.PARTNER_3RD_FLAG, "sharePOI");
        bundle.putSerializable(Constants.PARTNER_3RD_POI, poiItemInfo);
        intent.putExtras(bundle);
        startActivity(intent);
        finish();
    }
}
